package com.zhuge_new.Fragment;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.Response;
import com.tencent.mm.opensdk.modelmsg.SendMessageToWX;
import com.tencent.mm.opensdk.modelmsg.WXMediaMessage;
import com.tencent.mm.opensdk.modelmsg.WXWebpageObject;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;
import com.zhuge_new.Activity.AboutMiActivity;
import com.zhuge_new.Activity.AnearningsActivity;
import com.zhuge_new.Activity.CardExamineActivity;
import com.zhuge_new.Activity.DepositActivity;
import com.zhuge_new.Activity.GatheringAcivity;
import com.zhuge_new.Activity.ManageActivity;
import com.zhuge_new.Activity.MinShareActivity;
import com.zhuge_new.Activity.PersonageActivity;
import com.zhuge_new.Activity.PlanActivity;
import com.zhuge_new.Activity.PutForwordActivity;
import com.zhuge_new.Activity.RedExanmineActivity;
import com.zhuge_new.Activity.SafetyActivity;
import com.zhuge_new.Activity.SetActivity;
import com.zhuge_new.Base.BaseFragment;
import com.zhuge_new.R;
import com.zhuge_new.bean.PresentBean;
import com.zhuge_new.http.StringDialogCallback;
import com.zhuge_new.share.BaseHttp;
import com.zhuge_new.untils.DataParse;
import com.zhuge_new.untils.GlideUtils;
import com.zhuge_new.untils.NumberUtil;
import com.zhuge_new.untils.PreferencesUtils;
import com.zhuge_new.untils.StatusBarUtil;
import com.zhuge_new.wxapi.Constacts;

import org.json.JSONException;
import org.json.JSONObject;

/**
 * Created by lenovo on 2018-08-27.
 */

public class MineFragment extends BaseFragment implements View.OnClickListener {

    private TextView min_persent;
    private TextView min_accumulative;
    private TextView min_name;
    private ImageView min_sex;
    private TextView min_phone;
    private TextView rank;
    private String pass;
    private IWXAPI wxApi;
    private PresentBean presentBean;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_mine, container, false);
        return view;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        init_title();
        getData();
        getpersonage();
    }

    @Override
    public void init_title() {
        super.init_title();
        StatusBarUtil.transparencyBar(getActivity());
        StatusBarUtil.transparencyBar(getActivity());
        wxApi = WXAPIFactory.createWXAPI(getActivity(), Constacts.WXAPPID);
        wxApi.registerApp(Constacts.WXAPPID);
        LinearLayout min_plan = getActivity().findViewById(R.id.min_plan);
        LinearLayout min_manage = getActivity().findViewById(R.id.min_manage);
        LinearLayout min_card = getActivity().findViewById(R.id.min_card);
        LinearLayout min_red = getActivity().findViewById(R.id.min_red);
        LinearLayout min_min = getActivity().findViewById(R.id.min_min);
        LinearLayout min_safety = getActivity().findViewById(R.id.min_safety);
        LinearLayout min_set = getActivity().findViewById(R.id.min_set);
        ImageView min_personage = getActivity().findViewById(R.id.min_personage);
        LinearLayout min_onearnings = getActivity().findViewById(R.id.min_onearnings);
        LinearLayout min_put_forword = getActivity().findViewById(R.id.min_put_forword);
        LinearLayout min_share = getActivity().findViewById(R.id.min_share);
        LinearLayout min_addmoney = getActivity().findViewById(R.id.min_addmoney);
        LinearLayout min_gathering = getActivity().findViewById(R.id.min_gathering);
        LinearLayout min_deposit = getActivity().findViewById(R.id.min_deposit);
        RelativeLayout min_backround = getActivity().findViewById(R.id.min_backround);
        TextView min_hitphone=getActivity().findViewById(R.id.min_hitphone);
        min_persent = getActivity().findViewById(R.id.min_persent);
        min_accumulative = getActivity().findViewById(R.id.min_accumulative);
        min_name = getActivity().findViewById(R.id.min_name);
        min_sex = getActivity().findViewById(R.id.min_sex);
        min_phone = getActivity().findViewById(R.id.min_phone);
        rank = getActivity().findViewById(R.id.rank);
        ImageView min_headx = getActivity().findViewById(R.id.min_headx);
        String url = PreferencesUtils.getString(getActivity(), "wx_headimgurl");
        GlideUtils.loadcircle(getActivity(),url,min_headx,360,150,150);
        Bitmap bitmap = GlideUtils.loadBitmap(getActivity(), url);
        BitmapDrawable bd= new BitmapDrawable(getResources(), bitmap);
        setBackgroundOfVersion(min_backround,bd);
        min_share.setOnClickListener(this);
        min_put_forword.setOnClickListener(this);
        min_plan.setOnClickListener(this);
        min_manage.setOnClickListener(this);
        min_card.setOnClickListener(this);
        min_red.setOnClickListener(this);
        min_min.setOnClickListener(this);
        min_safety.setOnClickListener(this);
        min_set.setOnClickListener(this);
        min_onearnings.setOnClickListener(this);
        min_personage.setOnClickListener(this);
        min_addmoney.setOnClickListener(this);
        min_gathering.setOnClickListener(this);
        min_deposit.setOnClickListener(this);
        min_hitphone.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.min_plan:
                Intent intent1 = new Intent(getActivity(), PlanActivity.class);
                startActivity(intent1);
                break;
            case R.id.min_manage:
                Intent intent2 = new Intent(getActivity(), ManageActivity.class);
                startActivity(intent2);
                break;
            case R.id.min_card:
                Intent intent3 = new Intent(getActivity(), CardExamineActivity.class);
                startActivity(intent3);
                break;
            case R.id.min_red:
                Intent intent4 = new Intent(getActivity(), RedExanmineActivity.class);
                startActivity(intent4);
                break;
            case R.id.min_min:
                Intent intent5 = new Intent(getActivity(), AboutMiActivity.class);
                startActivity(intent5);
                break;
            case R.id.min_safety:
                Intent intent6 = new Intent(getActivity(), SafetyActivity.class);
                startActivity(intent6);
                break;
            case R.id.min_set:
                Intent intent7 = new Intent(getActivity(), SetActivity.class);
                intent7.putExtra("pass", pass);
                startActivity(intent7);
                break;
            case R.id.min_put_forword:
                Intent intent8 = new Intent(getActivity(), PutForwordActivity.class);
                startActivity(intent8);
                break;
            case R.id.min_onearnings:
                Intent intent9 = new Intent(getActivity(), AnearningsActivity.class);
                intent9.putExtra("type", "1");
                startActivity(intent9);
                break;
            case R.id.min_share:
                Intent minshare = new Intent(getActivity(), MinShareActivity.class);
                startActivity(minshare);
               /* sharewx(1);*/
                break;
            case R.id.min_personage:
                Intent intenta = new Intent(getActivity(), PersonageActivity.class);
                startActivity(intenta);
                break;
            case R.id.min_addmoney:
                Intent intent10 = new Intent(getActivity(), AnearningsActivity.class);
                intent10.putExtra("type", "2");
                startActivity(intent10);
                break;
            case R.id.min_deposit:
                Intent intent_deposit = new Intent(getActivity(), DepositActivity.class);
                startActivity(intent_deposit);
                break;
            case R.id.min_gathering:
                Intent intent_gathering = new Intent(getActivity(), GatheringAcivity.class);
                startActivity(intent_gathering);
                break;
            case R.id.min_hitphone:
                Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:"+"4001650899"));
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                startActivity(intent);
                break;
        }
    }


    @Override
    public void getData() {
        super.getData();
        String a = PreferencesUtils.getString(getActivity(), "unionid");
        OkGo.<String>post(BaseHttp.get_profits)
                .tag(this)
                .params("openId", PreferencesUtils.getString(getActivity(), "unionid"))
                .execute(new StringDialogCallback(getActivity()) {
                    @Override
                    public void onSuccessResponse(Response<String> response, String msg, String msgCode) {
                        presentBean = (PresentBean) new DataParse().getBean(response.body(), PresentBean.class, 0);
                        String persent = NumberUtil.saveRound(presentBean.getObject().getDayProfit(), 2);

                        String accumulative = String.valueOf(NumberUtil.saveRound(presentBean.getObject().getSumProfit(), 2));

                        if (accumulative.equals("00")) {
                            min_accumulative.setText("0.00");
                        } else {
                            min_persent.setText(accumulative);
                        }

                        if (persent.equals("00")) {
                            min_persent.setText("0.00");
                        } else {
                            min_persent.setText(persent);
                        }
                    }
                });
    }

    public void getpersonage() {
        OkGo.<String>post(BaseHttp.user_msg_data)
                .tag(this)
                .headers("token", PreferencesUtils.getString(getActivity(), "token"))
                .execute(new StringDialogCallback(getActivity()) {
                    @Override
                    public void onSuccessResponse(Response<String> response, String msg, String msgCode) {
                        try {
                            JSONObject data = new JSONObject(response.body());
                            rank.setText(data.getString("levelName"));
                            String isOpen = String.valueOf(data.get("isOpen"));

                            JSONObject userMsg = data.getJSONObject("userMsg");

                            min_name.setText(userMsg.getString("nickName"));
                            String sex = userMsg.getString("sex");
                            if (sex.equals("1")) {
                                min_sex.setImageDrawable(getResources().getDrawable(R.mipmap.male_select));
                            } else {
                                min_sex.setImageDrawable(getResources().getDrawable(R.mipmap.female_select));
                            }
                            min_phone.setText(userMsg.getString("mobile"));
                            pass = userMsg.getString("pass");

                            PreferencesUtils.putString(getActivity(), "name", userMsg.getString("nickName"));
                            PreferencesUtils.putString(getActivity(), "mobile", userMsg.getString("mobile"));
                            PreferencesUtils.putString(getActivity(), "userhead", userMsg.getString("userhead"));
                            PreferencesUtils.putString(getActivity(), "weixinqrode", userMsg.getString("weixinqrode"));
                            PreferencesUtils.putString(getActivity(), "pass", userMsg.getString("pass"));
                            PreferencesUtils.putString(getActivity(), "sex", userMsg.getString("sex"));
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                });


    }

    private void sharewx(int flag) {
        WXWebpageObject webpage = new WXWebpageObject();
        webpage.webpageUrl = "http://web.zgzngj.com";
        WXMediaMessage msg = new WXMediaMessage(webpage);
        msg.title = "诸葛智能管家";
        msg.description = "我发现一款超级好玩的APP,赶快下载抢红包";
        Bitmap thumb = BitmapFactory.decodeResource(getResources(), R.mipmap.logo);
        msg.setThumbImage(thumb);
        SendMessageToWX.Req req = new SendMessageToWX.Req();
        req.transaction = String.valueOf(System.currentTimeMillis());
        req.message = msg;
        req.scene = flag == 0 ? SendMessageToWX.Req.WXSceneSession : SendMessageToWX.Req.WXSceneTimeline;
        wxApi.sendReq(req);
    }

    private void setBackgroundOfVersion(View view, Drawable drawable) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {

            view.setBackground(drawable);
        } else {

            view.setBackgroundDrawable(drawable);
        }
    }


}
